
/*
 * PagoscomerciosFijosFrame.java
 *
 * Created on 13 de abril de 2008, 11:44 AM
 */
package org.comercios.registros;

import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import org.mybeans.ComerciosBeans;
import org.mybeans.ComerciosInscripcionBeans;
import org.mybeans.MorosidadMensualBeans;
import org.mybeans.PagosAnualesBeans;
import org.mybeans.PagosMensualesBeans;
import org.mybeans.RecargoMensualBeans;
import org.mybeans.RecibesBeans;
import org.mybeans.RecibesDetallesBeans;
import org.mybeans.RecibesNumeracionBeans;
import org.mybeans.RegimenBeans;
import org.mybeans.RegimenComerciosBeans;
import org.mybeans.Sesion;
import org.mybeans.VigenciasExpiradasBeans;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author  avbravo
 */
public class ComerciosBajaFrame extends javax.swing.JFrame {

    boolean focoComercio = false;
    boolean focoImpuesto = false;
    String reportSource;
    String reportDest;
    JTextField fieldPago;
    JTextField fieldSaldo;
    JTextField fieldExoneracion;
    JPopupMenu popupMenu = new JPopupMenu("Menu");
    JMenuItem menuPagar = new JMenuItem("Pagar");
    JMenuItem menuExonerar = new JMenuItem("Exonerar");
    JSeparator separator0 = new javax.swing.JSeparator();
    JMenuItem menuCerrar = new JMenuItem("Cerrar");
    JMenuItem menuEliminar = new JMenuItem("Eliminar");
    Sesion sesion;
    RegimenBeans regimenBeans;
    ComerciosBeans comerciosBeans;
    ComerciosInscripcionBeans comerciosInscripcionBeans;
    RegimenComerciosBeans regimenComerciosBeans;
    PagosMensualesBeans pagosMensualesBeans;
    PagosAnualesBeans pagosAnualesBeans;
    MorosidadMensualBeans morosidadMensualBeans;
    RecargoMensualBeans recargoMensualBeans;
    VigenciasExpiradasBeans vigenciasExpiradasBeans;
    RecibesBeans recibesBeans;
    RecibesDetallesBeans recibesDetallesBeans;
    RecibesNumeracionBeans recibesNumeracionBeans;
    int fila = -1;
    DefaultTableModel modelo;
    private int AnioRecibo;
    private int NumeroMes;
    String IdComercio;
    String TipoId = "comercios";
    private String idRegimen;
    String sql;
    String rowIdRegimen;
    String rowRegimen;
    double rowDeuda;
    double rowPago;
    double rowSaldo;
    double rowExoneracion;
    String rowTipo;
    String rowDetalle;
    double rowValor;
    double rowMorosidad;
    String rowComentario;
    int rowCantidad;
    double Pago = 0;
    double Exoneracion = 0;
    double Valor = 0.0;
    double Saldo = 0.0;
    double SaldoAnterior = 0.0;
    int IdRecibo = 0;
    Double TotalPagado = 0.0;
    Double TotalExonerado = 0.0;
    String idExoneracion = "";
    Double Deuda;
    boolean TieneExoneracion = false;
    boolean TieneAlgunRegistroMoroso = false;
    String Estatus;
    int n;
    int paginas;
    String NoPaga = "no";
    String Motivo = "";

    /** Creates new form */
    public ComerciosBajaFrame() {
        initComponents();
        sesion = new Sesion();
        /*
         * Fondo
         */

         
         
         

        jPanelFondo.setOpaque(false);
        setContentPane(jPanelFondo);
        /*
         * fondo
         */

        regimenBeans = new RegimenBeans();
        comerciosBeans = new ComerciosBeans();
        comerciosInscripcionBeans = new ComerciosInscripcionBeans();
        regimenComerciosBeans = new RegimenComerciosBeans();
        pagosMensualesBeans = new PagosMensualesBeans();
        pagosAnualesBeans = new PagosAnualesBeans();
        morosidadMensualBeans = new MorosidadMensualBeans();
        recargoMensualBeans = new RecargoMensualBeans();
        vigenciasExpiradasBeans = new VigenciasExpiradasBeans();
        recibesBeans = new RecibesBeans();
        recibesDetallesBeans = new RecibesDetallesBeans();
        recibesNumeracionBeans = new RecibesNumeracionBeans();
        java.util.Calendar ca = java.util.Calendar.getInstance();
        java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());
        jFormattedTextFieldFecha.setValue(mydate);
        sesion.setTipoId("comercios");

        this.AnioRecibo = ca.get(java.util.Calendar.YEAR);
        NumeroMes = ca.get(java.util.Calendar.MONTH);

        sesion.setAnioRecibo(AnioRecibo);
        sesion.setNumeroMesRecibo(NumeroMes);
        //


        if (sesion.getIdComercio() == null || sesion.getIdComercio().equals("")) {
        } else {
            jTextFieldIdComercio.setText(sesion.getIdComercio());
            ValidarComercio();
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelFondo = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jFormattedTextFieldFecha = new javax.swing.JFormattedTextField();
        jButtonRegresar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldCedula = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldContribuyente = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextFieldIdComercio = new javax.swing.JTextField();
        jTextFieldComercio = new javax.swing.JTextField();
        jButtonBuscar = new javax.swing.JButton();
        jButtonBajar = new javax.swing.JButton();
        jButtonLimpiar = new javax.swing.JButton();
        jButtonRegresar2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldAutorizado = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaMotivo = new javax.swing.JTextArea();
        jButtonCertificacion = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("BAJA DE COMERCIOS");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanelFondo.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanelFondo.setOpaque(false);
        jPanelFondo.setPreferredSize(new java.awt.Dimension(1007, 800));

        jLabel3.setText("Fecha");

        jFormattedTextFieldFecha.setEditable(false);
        jFormattedTextFieldFecha.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));
        jFormattedTextFieldFecha.setToolTipText("Fecha");
        jFormattedTextFieldFecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFormattedTextFieldFechaActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jLabel4.setText("Cedula");

        jTextFieldCedula.setColumns(35);
        jTextFieldCedula.setEditable(false);
        jTextFieldCedula.setToolTipText("Cedula del contribuyente");
        jTextFieldCedula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldCedulaControlActionPerformed(evt);
            }
        });
        jTextFieldCedula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldCedulaKeyPressed(evt);
            }
        });

        jLabel5.setText("Contribuyente");

        jTextFieldContribuyente.setColumns(70);
        jTextFieldContribuyente.setEnabled(false);

        jLabel7.setText("Comercio");

        jLabel8.setText("Codigo");

        jTextFieldIdComercio.setColumns(35);
        jTextFieldIdComercio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldIdComercioActionPerformed(evt);
            }
        });

        jTextFieldComercio.setEditable(false);

        jButtonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/search.png"))); // NOI18N
        jButtonBuscar.setToolTipText("Buscar");
        jButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarActionPerformed(evt);
            }
        });

        jButtonBajar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/save.png"))); // NOI18N
        jButtonBajar.setToolTipText("Guardar Baja");
        jButtonBajar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBajarActionPerformed(evt);
            }
        });

        jButtonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/escoba.png"))); // NOI18N
        jButtonLimpiar.setToolTipText("Limpiar");
        jButtonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimpiarActionPerformed(evt);
            }
        });

        jButtonRegresar2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar2.setToolTipText("Regresar");
        jButtonRegresar2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresar2ActionPerformed(evt);
            }
        });

        jLabel1.setText("Autorizado por");

        jLabel2.setText("Motivo de Baja");

        jTextAreaMotivo.setColumns(20);
        jTextAreaMotivo.setRows(5);
        jScrollPane1.setViewportView(jTextAreaMotivo);

        jButtonCertificacion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/certificacion.jpeg"))); // NOI18N
        jButtonCertificacion.setToolTipText("Certificacion");
        jButtonCertificacion.setEnabled(false);
        jButtonCertificacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCertificacionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelFondoLayout = new javax.swing.GroupLayout(jPanelFondo);
        jPanelFondo.setLayout(jPanelFondoLayout);
        jPanelFondoLayout.setHorizontalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(29, 29, 29)
                        .addComponent(jTextFieldIdComercio, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldComercio, javax.swing.GroupLayout.PREFERRED_SIZE, 565, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(jButtonBajar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCertificacion, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonRegresar2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jFormattedTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldContribuyente, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldAutorizado, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(50, Short.MAX_VALUE))
        );
        jPanelFondoLayout.setVerticalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(jButtonRegresar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jTextFieldComercio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jTextFieldIdComercio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonBuscar))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldContribuyente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jFormattedTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldAutorizado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jButtonBajar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonCertificacion, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE))
                    .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonRegresar2, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelFondo, javax.swing.GroupLayout.PREFERRED_SIZE, 692, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelFondo, javax.swing.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed

    this.dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jFormattedTextFieldFechaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFormattedTextFieldFechaActionPerformed
}//GEN-LAST:event_jFormattedTextFieldFechaActionPerformed

private void jButtonRegresar2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresar2ActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresar2ActionPerformed

private void jButtonBajarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBajarActionPerformed


    try {
        if (ValidarEntradas() == false) {
            return;
        }



        //Ahora guardar el detalle del recibo


        GuardarBaja();




    } catch (Exception ex) {
        sesion.MostrarMensajeError("Error ", "Clase: PagosComerciosEventualesFrame.java\nActualizarMorosidad()\n " + ex.getMessage().toString());
    }

}//GEN-LAST:event_jButtonBajarActionPerformed

private void jButtonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimpiarActionPerformed

    Limpiar();

}//GEN-LAST:event_jButtonLimpiarActionPerformed

    private void ActualizarComercios() {
        try {
            CargarComercios();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
private void jTextFieldCedulaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldCedulaKeyPressed
}//GEN-LAST:event_jTextFieldCedulaKeyPressed

private void jTextFieldCedulaControlActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldCedulaControlActionPerformed
}//GEN-LAST:event_jTextFieldCedulaControlActionPerformed

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    // TODO add your handling code here:
    try {


        if (focoComercio) {
            ActualizarComercios();
        }

        focoComercio = false;
    } catch (Exception ex) {
        sesion.MostrarMensajeError("Error ", "Clase: PagosComerciosEventualesFrame.java\nBoton Actualizar Impuestos()\n " + ex.getMessage().toString());
    }
}//GEN-LAST:event_formWindowGainedFocus

private void jTextFieldIdComercioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldIdComercioActionPerformed
    ValidarComercio();
}//GEN-LAST:event_jTextFieldIdComercioActionPerformed

private void jButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarActionPerformed

    ComerciosBusquedaFrame comerciosBusquedaFrame = new ComerciosBusquedaFrame();
    comerciosBusquedaFrame.setLocationRelativeTo(null);
    comerciosBusquedaFrame.setVisible(true);
    focoComercio = true;
    focoImpuesto = false;

}//GEN-LAST:event_jButtonBuscarActionPerformed

private void jButtonCertificacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCertificacionActionPerformed


    String idComercio = this.jTextFieldIdComercio.getText();
    if (idComercio == null || idComercio.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese el codigo ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldIdComercio.requestFocus();
        return;
    }

    String comercio = this.jTextFieldComercio.getText();
    if (comercio == null || comercio.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese el comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldComercio.requestFocus();
        return;
    }

    String reportSource;
    reportSource = sesion.getRutaReportes() + "ComerciosBajaCertificacion.jrxml";
    String reportDest;
    reportDest = sesion.getRutaDestino() + "ComerciosBajaCertificacion.html";

    Map<String, Object> params = new HashMap<String, Object>();

    try {
        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        JasperReport jasperReport =
                JasperCompileManager.compileReport(reportSource);
        params.put("P_IDMUNICIPIO", sesion.getIdmunicipio());
        params.put("P_IDCOMERCIO", idComercio);
        params.put("P_MUNICIPIO", sesion.getMunicipio());
        params.put("P_NOMBREUSUARIO", sesion.getUsuario());
        JasperPrint jasperPrint =
                JasperFillManager.fillReport(
                jasperReport, params, sesion.con);

        JasperViewer.viewReport(jasperPrint, false);

    } catch (JRException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonCertificacionActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ComerciosBajaFrame().setVisible(true);
            }
        });
    }

    /*
     * 
     *verifica si ya se pago la placa para este año 
     * 
     */
    private void LimpiarComercios() {
        jTextFieldIdComercio.setText("");
        jTextFieldComercio.setText("");
        jTextFieldCedula.setText("");
        jTextFieldContribuyente.setText("");
    }

    private void Limpiar() {
        try {

            LimpiarComercios();
            jTextAreaMotivo.setText("");
            jTextFieldAutorizado.setText("");
            jButtonCertificacion.setEnabled(false);

        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "Metodo: Limpiar()\n " + ex.getMessage().toString());
        }
    }

    private boolean ValidarEntradas() {
        try {


            IdComercio = this.jTextFieldIdComercio.getText();

            if (IdComercio == null || IdComercio.equals("")) {
                JOptionPane.showMessageDialog(this, "No ha seleccionado un comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldIdComercio.requestFocus();
                return false;
            }


            String Contribuyente = this.jTextFieldContribuyente.getText();
            if (Contribuyente == null || Contribuyente.equals("")) {
                JOptionPane.showMessageDialog(this, "No ha seleccionado ningun contribuyente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldContribuyente.requestFocus();
                return false;
            }



            return true;
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "Metodo:ValidarEntradas()\n " + ex.getMessage().toString());
        }
        return false;
    }

    private void ValidarComercio() {
        try {
            jButtonCertificacion.setEnabled(false);
            String idComercio = this.jTextFieldIdComercio.getText();
            comerciosBeans.setIdComercio(idComercio);

            if (comerciosBeans.Consultar() == true) {
                CargarComercios();
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Metodo:ValidarComercio()\nError " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void CargarComercios() {
        try {
            if (comerciosBeans.getActivo().equals("no")) {
                JOptionPane.showMessageDialog(this, "El comercio no esta activo", "Mensaje", JOptionPane.WARNING_MESSAGE);
                return;
            }
            String tmpIdComercio = comerciosBeans.getIdComercio();
            if (comerciosBeans.ConsultarEstaBajado(tmpIdComercio) == true) {
                JOptionPane.showMessageDialog(this, "El comercio esta bajado", "Mensaje", JOptionPane.WARNING_MESSAGE);
                return;
            }
            this.jTextFieldIdComercio.setText(comerciosBeans.getIdComercio());
            this.jTextFieldComercio.setText(comerciosBeans.getComercio());
            this.jTextFieldCedula.setText(comerciosBeans.getCedula());
            this.jTextFieldContribuyente.setText(comerciosBeans.getContribuyente());
            sesion.setIdComercio(jTextFieldIdComercio.getText());
            jButtonBajar.setEnabled(true);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void GuardarBaja() {
        try {


            String motivo = this.jTextAreaMotivo.getText();
            if (motivo == null || motivo.equals("")) {
                JOptionPane.showMessageDialog(this, "ingrese el motivo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextAreaMotivo.requestFocus();
                return;
            }
            String autorizado = this.jTextFieldAutorizado.getText();
            if (autorizado == null || autorizado.equals("")) {
                JOptionPane.showMessageDialog(this, "ingrese el nombre de quien autoriza", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jTextFieldAutorizado.requestFocus();
                return;
            }
            int res = JOptionPane.showConfirmDialog(this, "Desea dar de baja este comercio",
                    "Bajar Comercio ", JOptionPane.YES_NO_OPTION);
            if (res == JOptionPane.YES_OPTION) {

                if (sesion.Conectar() == false) {
                    JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                String sql = "INSERT into comerciosbaja  VALUES(?,?,?,?,?,?,?)";
                PreparedStatement prest = sesion.con.prepareStatement(sql);

                prest.setString(1, this.IdComercio); //
                prest.setString(2, "si"); //activo
                prest.setString(3, motivo);//motivo
                prest.setString(4, autorizado); //autorizado
                prest.setString(5, sesion.getIdmunicipio());
                java.util.Calendar ca = java.util.Calendar.getInstance();
                java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());
                prest.setDate(6, mydate);
                prest.setString(7, sesion.getUsername());


                int count = prest.executeUpdate();
                sesion.con.close();
                ActualizarEstatusComercio();
                JOptionPane.showMessageDialog(this, "Baja del comercio registrada exitosamente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                //  Limpiar();
                jButtonBajar.setEnabled(false);
                jButtonCertificacion.setEnabled(true);
            } else {
                JOptionPane.showMessageDialog(this, "No se bajo el comercio ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            System.out.println("Error" + ex);
            JOptionPane.showMessageDialog(this, "Metodo:GuardarBaja()\nError " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }

    }

    private void ActualizarEstatusComercio() {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            String idComercio = jTextFieldIdComercio.getText();
            String sql = "update comercios set activo ='no' where idmunicipio =? and idcomercio=? and activo='si'";

            PreparedStatement prest = sesion.con.prepareStatement(sql);

            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, idComercio);

            int count = prest.executeUpdate();
            sesion.con.close();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Metodo:ActualizarEstatusComercio()\nError " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBajar;
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JButton jButtonCertificacion;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JButton jButtonRegresar2;
    private javax.swing.JFormattedTextField jFormattedTextFieldFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanelFondo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaMotivo;
    private javax.swing.JTextField jTextFieldAutorizado;
    private javax.swing.JTextField jTextFieldCedula;
    private javax.swing.JTextField jTextFieldComercio;
    private javax.swing.JTextField jTextFieldContribuyente;
    private javax.swing.JTextField jTextFieldIdComercio;
    // End of variables declaration//GEN-END:variables
}
